﻿
using System.Text;
namespace LFSRecord2.Misc
{
    public static class FileHelper
    {
        public static byte[] StringToByteArray(string str)
        {
            byte[] b = new byte[str.Length];
            for (int a = 0; a < str.Length; a++)
            {
                b[a] = (byte)str[a];
            }
            return b;
        }

        public static string CharArrayToString(char[] input, bool filterAllBinary)
        {
            for (int i = 0; i < input.Length; i++)
            {
                if ((filterAllBinary && input[i] < 32) ||
                    input[i] == 0)
                {
                    return new string(input, 0, i);
                }
            }
            return new string(input);
        }

        public static string CharArrayToString(byte[] input, bool filterAllBinary)
        {
            char[] c = new char[input.Length];
            for (int i = 0; i < input.Length; i++)
            {
                if ((filterAllBinary && input[i] < 32) ||
                    input[i] == 0)
                {
                    return new string(c, 0, i);
                }
                c[i] = (char)input[i];
            }
            return new string(c);
        }

        public static string CharArrayToString(char[] input)
        {
            return CharArrayToString(input, false);
        }

        public static string ByteArrayToString(byte[] input)
        {
            return CharArrayToString(input, false);
        }

        public static string ByteArrayToUtf8(byte[] input)
        {
            int a;

            int numRealBytes = 0;
            for (a = 0; a < input.Length; a++)
            {
                if (input[a] == 0)
                    break;
                numRealBytes++;
            }

            Encoding encoding = new UTF8Encoding();
            return encoding.GetString(input, 0, numRealBytes);
        }
    }
}
